#include <QtGui>
#include "diagramWindow.h"

DiagramWindow::DiagramWindow()
{
  scene = new QGraphicsScene(0, 0, 600, 500);
  view = new QGraphicsView;
  view->setScene(scene);
  view->setDragMode(QGraphicsView::RubberBandDrag);
  view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  view->setContextMenuPolicy(Qt::ActionsContextMenu);
  setCentralWidget(view);
  
  minZ = 0;
  maxZ = 0;
  seqNumber = 0;

  //createActions();
  //createMenus();
  //createToolBars();

  connect(scene, SIGNAL(selectionChanged()), this, SLOT(updateActions()));

  setWindowTitle(tr("Diagram"));
  updateActions();
  addNode();
  Node *node1 = selectedNode();
  
  addNode();
  Node *node2 = selectedNode();
 
  node1->setSelected(true);
  node2->setSelected(true);
 
  addLink();  

  addPort();
}
DiagramWindow::~DiagramWindow()
{
  delete scene;
  delete view;  
}

void DiagramWindow::addNode()
{
  Node *node = new Node;
  node->setText(tr("Node %1").arg(seqNumber+1));
  setupNode(node);
}

void DiagramWindow::addPort()
{
  Port *port = new Port;
  setupPort(port);
}

void DiagramWindow::setupNode(Node *node)
{
  node->setPos(QPoint(80 + (100 *(seqNumber%5)),
                      80 + (50 * ((seqNumber/5)%7))));
  scene->addItem(node);
  ++seqNumber;

  scene->clearSelection();
  node->setSelected(true);
  bringToFront();
}

void DiagramWindow::setupPort(Port *port)
{
  if (!port ) return;

  //port->setPos(QPoint(80 + (100 *(seqNumber%5)),
   //                   80 + (50 * ((seqNumber/5)%7))));
  scene->addItem(port);
  ++seqNumber;

  scene->clearSelection();
  port->setSelected(true);
}

void DiagramWindow::bringToFront()
{
  ++maxZ;
  setZValue(maxZ);
}

void DiagramWindow::sendToBack()
{
  --minZ;
  setZValue(minZ);
}

void DiagramWindow::setZValue(int z)
{
  Node *node = selectedNode();
  if (node )
    node->setZValue(z);
}

Node *DiagramWindow::selectedNode() const
{
  QList<QGraphicsItem *> items = scene->selectedItems();
  if ( items.count() == 1 ) {
    return dynamic_cast<Node*> (items.first());
  }else {
    return NULL;
  }
}

Link *DiagramWindow::selectedLink() const
{
  QList<QGraphicsItem *> items = scene->selectedItems();
  if ( items.count() == 1 ) {
    return dynamic_cast<Link*> (items.first());
  }else {
    return NULL;
  }
}

void DiagramWindow::addLink()
{
  NodePair nodes = selectedNodePair();
  if (nodes == NodePair())
    return;
  
  Link *link = new Link(nodes.first, nodes.second);
  scene->addItem(link);
}

DiagramWindow::NodePair DiagramWindow::selectedNodePair() const
{
  QList<QGraphicsItem *> items = scene->selectedItems();
  if (items.count() == 2) {
    Node *first = dynamic_cast<Node *> (items.first());
    Node *second = dynamic_cast<Node *> (items.last());
    if ( first && second )
      return NodePair(first, second);
  }
  return NodePair();
} 

void DiagramWindow::del()
{
  QList<QGraphicsItem *> items = scene->selectedItems();
  QMutableListIterator<QGraphicsItem *> i(items);
  while (i.hasNext()) {
    Link *link = dynamic_cast<Link *> (i.next());
    if(link) {
      delete link;
      i.remove();
    }
  }
  qDeleteAll(items);
}

void DiagramWindow::properties()
{
  Node *node = selectedNode();
  Link *link = selectedLink();
  
  if(node) {
//   PropertiesDialog dialog(node, this);
//   dialog.exec();
  } else if (link) {
   QColor color = QColorDialog::getColor(link->color(), this);
   if ( color.isValid() ) link->setColor(color);
  }
}

void DiagramWindow::cut()
{
  Node *node = selectedNode();
  if(!node ) return;
  
  copy();
  delete node;
}

void DiagramWindow::copy()
{
  Node *node = selectedNode();
  if (!node ) return;

  QString str = QString("Node %1 %2 %3 %4")
                .arg(node->textColor().name())
                .arg(node->outlineColor().name())
                .arg(node->backgroundColor().name())
                .arg(node->text());
  QApplication::clipboard()->setText(str);
}

void DiagramWindow::paste()
{
  QString str = QApplication::clipboard()->text();
  QStringList parts = str.split(" ");
  if (parts.count() >= 5 && parts.first() == "Node") {
    Node *node = new Node;
    node->setText(QStringList(parts.mid(4)).join(" "));
    node->setTextColor(QColor(parts[1]));
    node->setOutlineColor(QColor(parts[2]));
    node->setBackgroundColor(QColor(parts[3]));
    setupNode(node);
  }
}

void DiagramWindow::updateActions()
{
  /*
  bool hasSelection = !scene->selectedItems().isEmpty();
  bool isNode = (selectedNode() != NULL);
  bool isNodePair = (selectedNodePair() != NodePair());
  */
  //cutAction
}
                
